package secuDropper.core.util;

import java.util.Arrays;

/**
 * @author Matthias Hauser
 */
public class StringUtils {

	/**
	 * Concatenates a nullstring to the end of this string if str.length() <
	 * len, shortens this string to the given len otherwise. <br>
	 * <br>
	 * Example:
	 * 
	 * <pre>
	 * String test = &quot;hello&quot;;
	 * String padded_0 = padnulls(test, 8); // gives &quot;hello   &quot;
	 * String padded_1 = padnulls(test, 3); // gives &quot;hel&quot;
	 * </pre>
	 * 
	 * @param str
	 *            string to pad with nulls
	 * @param len
	 *            length of the resulting string
	 * @return a string, so that
	 * 
	 *         <pre>
	 * padnulls(str, len).length() == len
	 * </pre>
	 */
	public static String padnulls(String str, int len) {
		return str.concat(nullstring(len)).substring(0, len);
	}

	public static String nullstring(int len) {
		byte zero = 0;
		byte[] nulls = new byte[len];
		Arrays.fill(nulls, zero);
		return new String(nulls);
	}

}
